package org.example.nio.reactormodel;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * 新连接处理器
 *
 * @author hh
 * @since 2024/10/27
 */
public class AcceptorHandler implements Runnable{

    private ServerSocketChannel serverSocketChannel;
    private Selector selector;

    public AcceptorHandler(ServerSocketChannel serverSocketChannel, Selector selector) {
        this.serverSocketChannel = serverSocketChannel;
        this.selector = selector;
    }

    @Override
    public void run() {
        try {
            SocketChannel channel = serverSocketChannel.accept();
            // 创建传输处理器，并且将传输通道注册到选择器2
            if (channel != null) {
                new MultiThreadEchoHandler(selector, channel);
            }
            System.out.println("新连接处理器处理...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
